package com.koron.tool.frame;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.sql.*;
import java.util.ResourceBundle;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import com.koron.tool.Constant;
import com.koron.tool.MainFrame;
import com.koron.tool.dialog.BeanDialog;
/**
 * 由SQL语句生成java BEAN
 * @author swan
 *
 */
public class SqlBeanAction extends AbstractAction {
    private MainFrame frame;
    private ResourceBundle resourceBuddle = null;

    public SqlBeanAction(MainFrame frame) {
        this.frame = frame;
        resourceBuddle = ResourceBundle.getBundle(MainFrame.RESOURCEBUDDLE, frame.getLocale());
        putValue(NAME, "SQL生成BEAN");
        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK+InputEvent.ALT_MASK));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String str = JOptionPane.showInputDialog(frame, resourceBuddle.getString("InputSQL"));
        if (str != null && !str.equals("")) {
            if (str.indexOf("where") == -1) {
                str += " where 1=2";
            } else {
                str += " and 1=2";
            }
            Connection conn = null;
            try{ 
                conn = MainFrame.getConnection(Constant.DEVELOPDB);
                ResultSet rs = conn.createStatement().executeQuery(str);
                ResultSetMetaData metaData = rs.getMetaData();
                String[][] fields = new String[metaData.getColumnCount()][4];
                for (int i = 0; i < fields.length; i++) {
                    fields[i][0] = "private";
                    int type = metaData.getColumnType(i + 1);
                    switch (type) {
                    case Types.INTEGER:
                    case Types.SMALLINT:
                    case Types.TINYINT:
                        fields[i][1] = "Integer";
                        break;
                    case Types.BIT:
                    case Types.BOOLEAN:
                        fields[i][1] = "Boolean";
                        break;
                    case Types.FLOAT:
                    case Types.DOUBLE:
                        fields[i][1] = "Double";
                        break;
                    case Types.DATE:
                    case Types.TIMESTAMP:
                        fields[i][1] = "Timestamp";
                        break;
                    default:
                        fields[i][1] = "String";
                        break;
                    }
                    fields[i][2] = metaData.getColumnLabel(i + 1);
                    fields[i][3] = "";
                    ResultSet rs2 = conn.getMetaData().getColumns(null, null, metaData.getTableName(i + 1),
                            metaData.getColumnName(i + 1));
                    if (rs2.next()) {
                        String tmp = rs2.getString("REMARKS");
                        if (tmp != null)
                            fields[i][3] = tmp;
                    }
                    rs2.close();
                }
                rs.close();
                new BeanDialog(frame, fields).setVisible(true);
            } catch (SQLException | ClassNotFoundException e1) {
                e1.printStackTrace();
                JOptionPane.showMessageDialog(frame, resourceBuddle.getString("DBException") + ":" + e1.getMessage());
            } finally {
                try {
                    if (conn != null)
                        conn.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

}
